/**
 * Copyright (C), 2015-2020, XXX有限公司
 * FileName: BusCollectActivity
 * Author: Clark
 * Date: 2020/10/10 21:01
 * Description:
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 描述
 */
package sz.szsmk.citizencard.ui.activity;

import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.widget.ViewPager2;

import com.chenenyu.router.annotation.Route;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.ActivityCardListBinding;
import sz.szsmk.citizencard.databinding.ActivityCollectionlistBinding;
import sz.szsmk.citizencard.dialog.NetRequestTipsDialog;
import sz.szsmk.citizencard.ioc.Compontent.DaggerCommonActivityComponent;
import sz.szsmk.citizencard.model.network.dzbean.res.MessageEvent;
import sz.szsmk.citizencard.mvp.contract.ISimpleContact;
import sz.szsmk.citizencard.mvp.presenter.SimplePresenter;
import sz.szsmk.citizencard.ui.adapter.BusCollectFragmentAdapter;
import sz.szsmk.citizencard.ui.adapter.CardListFragmentAdapter;
import sz.szsmk.citizencard.ui.base.IBaseActivity;
import sz.szsmk.citizencard.ui.fragment.BusCollecLineFragment;
import sz.szsmk.citizencard.ui.fragment.BusHomeSiteCollecFragment;
import sz.szsmk.citizencard.ui.fragment.CardListCardsFragment;
import sz.szsmk.citizencard.ui.fragment.CardListCertificatesFragment;
import sz.szsmk.citizencard.ui.fragment.CardListTicketsFragment;
//import sz.szsmk.citizencard.ui.newversion.fragment.CardListCertificatesFragment;
//import sz.szsmk.citizencard.ui.newversion.fragment.CardListCouponFragment;
//import sz.szsmk.citizencard.ui.newversion.fragment.CardListTicketFragment;

import static java.util.Objects.*;

/**
 * @ClassName: BusCollectActivity
 * @Description:
 * @Author: Clark
 * @Date: 2020/10/10 21:01
 */
@RequiresApi(api = Build.VERSION_CODES.M)
@Route("cardListActivity")
public
class CardListActivity extends IBaseActivity<SimplePresenter, ActivityCardListBinding> implements ISimpleContact.ISimpleView, ViewPager2.OnScrollChangeListener, View.OnClickListener {


    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_card_list;
    }

    @Override
    protected void initInjector() {
        DaggerCommonActivityComponent.builder().appCompontent(DaggerApplication.getAppComponent())
                .build().inject(this);
    }

    @Override
    protected void initViews() {

//        showLoadingDialog();
        dataBinding.backLayout.settingTitle.setText("卡包");
        dataBinding.backLayout.back.setOnClickListener(this);
        List<Fragment> mFragments = new ArrayList<>();
        mFragments.add(new CardListCardsFragment());
        mFragments.add(new CardListTicketsFragment());
        mFragments.add(new CardListCertificatesFragment());
        CardListFragmentAdapter cardListFragmentAdapter = new CardListFragmentAdapter(this, mFragments, getSupportFragmentManager());
        ViewPager viewPager = dataBinding.viewPager;
        dataBinding.viewPager.setAdapter(cardListFragmentAdapter);
        dataBinding.viewPager.setOnScrollChangeListener(this);
        String[] mFilter = {
                getString(R.string.coupon),
                getString(R.string.ticket),
                getString(R.string.cards)};
        dataBinding.cardFilter.setViewPager((dataBinding.viewPager), mFilter);

    }

    @Override
    protected void updateViews() {

    }

    @Override
    public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {

    }

    @Subscribe(threadMode = ThreadMode.BACKGROUND)
    public void onMessageEvent(MessageEvent event) {
        if (event.getCode() == MessageEvent.CODE_SHOW_DIALOG) {
//            showLoadingDialog();
        } else if (event.getCode() == MessageEvent.CODE_DISSMISS_DIALOG) {
            Log.d("clark","dismissdialog");
//            dismissLoadingDialog();
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.back) {
            finish();
        }
    }

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
    NetRequestTipsDialog mNetRequestTipsDialog;
    public void showLoadingDialog() {
        if (null == mNetRequestTipsDialog) {
            mNetRequestTipsDialog = new NetRequestTipsDialog();
        }

        android.app.FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.add(mNetRequestTipsDialog, this.getClass().getSimpleName());
        ft.commitAllowingStateLoss();
    }
    public void dismissLoadingDialog() {
        if (null != mNetRequestTipsDialog) {
            mNetRequestTipsDialog.dismissAllowingStateLoss();
            mNetRequestTipsDialog = null;
        }
    }

}